﻿NotInheritable Class RssHelper

    'Retrieves feeds from the specified URL and returns an IEnumerable collection
    Shared Function RetrieveFeeds(ByVal Address As String) As IEnumerable(Of BlogPost)

        Dim doc As XDocument = XDocument.Load(Address)

        Dim query = From item In doc...<item> _
        Let DatePublished = CDate(item.<pubDate>.Value).ToLocalTime _
        Let PostTitle = item.<title>.Value _
        Let Url = item.<link>.Value _
        Let Category = item.<category>.Value _
            Order By DatePublished Descending _
            Select New BlogPost With _
            {.DatePublished = DatePublished, .Title = EscapeXml(PostTitle), _
             .Url = New Uri(Url), .Category = Category}

        Return query
    End Function

    'Special characters in RSS Feeds are automatically cleaned by LINQ-to-Xml but often they
    'are double encoded, so this method filters feeds again by replacing
    'some of the special characters with corresponding text.
    Shared Function EscapeXml(ByVal DirtyXml As String) As String

        DirtyXml = DirtyXml.Replace("&amp;", "&")
        DirtyXml = DirtyXml.Replace("&lt:", "<")
        DirtyXml = DirtyXml.Replace("&gt", ">")
        DirtyXml = DirtyXml.Replace("&quot;", """")
        DirtyXml = DirtyXml.Replace("&apos;", "'")
        DirtyXml = DirtyXml.Replace("&ograve;", "ò")
        DirtyXml = DirtyXml.Replace("&egrave;", "è")
        DirtyXml = DirtyXml.Replace("&ugrave;", "ù")
        DirtyXml = DirtyXml.Replace("&agrave;", "à")
        DirtyXml = DirtyXml.Replace("&hellip;", "...")

        Return DirtyXml
    End Function



End Class

